//
//  LDMapViewController.m
//  CoreData
//
//  Created by dllo on 15/11/21.
//  Copyright (c) 2015年 蓝鸥3g学院. All rights reserved.
//

#import "LDMapViewController.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>
#import <BaiduMapAPI_Map/BMKMapView.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
@interface LDMapViewController ()<BMKGeoCodeSearchDelegate, BMKMapViewDelegate>

@property(nonatomic, strong) BMKMapView *mapView;
@property(nonatomic, strong) BMKGeoCodeSearch *search;
@property(nonatomic, strong) NSString *mapStr;
@property(nonatomic, strong) BMKPointAnnotation * annotation;

@end

@implementation LDMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, 375, 667)];
    [self.view addSubview:self.mapView];
    self.mapView.delegate = self;
    
    
    //地理编码
    self.search = [[BMKGeoCodeSearch alloc] init];
    self.search.delegate = self;
    BMKGeoCodeSearchOption *geoCodeSearchOption = [[BMKGeoCodeSearchOption alloc]init];
    
//    geoCodeSearchOption.city= self.str;
    geoCodeSearchOption.address = self.str;
    BOOL flag = [self.search geoCode:geoCodeSearchOption];

    if(flag)
    {
        NSLog(@"geo检索发送成功");
    }
    else
    {
        NSLog(@"geo检索发送失败");
    }
    
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self.mapView addGestureRecognizer:longPress];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(click:)];
    
}

- (void)click:(UIBarButtonItem *)button {
    [self.delegate takeValue:self.mapStr];
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)longPress:(UIGestureRecognizer *)gestureRecognizer {
    CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];//这里touchPoint是点击的某点在地图控件中的位置
    CLLocationCoordinate2D touchMapCoordinate =
    [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];//这里touchMapCoordinate就是该点的经纬度了
    
    CLLocationCoordinate2D pt = (CLLocationCoordinate2D){touchMapCoordinate.latitude, touchMapCoordinate.longitude};
    BMKReverseGeoCodeOption *reverseGeoCodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
    reverseGeoCodeSearchOption.reverseGeoPoint = pt;
    [self.search reverseGeoCode:reverseGeoCodeSearchOption];

}

- (void)mapView:(BMKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
    
}

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation {
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BMKPinAnnotationView *pinView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"BMKPinAnnotationView"];
        if (pinView == nil) {
            //没有创建新的
            pinView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"BMKPinAnnotationView"];
        }
        //是否有气泡
        pinView.canShowCallout = YES;
        //是否有掉落的动画
        pinView.animatesDrop = YES;
        //颜色
        pinView.pinColor = BMKPinAnnotationColorPurple;
        
        UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        redView.backgroundColor = [UIColor redColor];
        pinView.leftCalloutAccessoryView = redView;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
        button.frame = CGRectMake(0, 0, 30, 30);
        pinView.rightCalloutAccessoryView = button;
        self.annotation = annotation;
        [self performSelector:@selector(showCallout) withObject:self afterDelay:0.1];
        return pinView;
        
    }
    return nil;

}


- (void)showCallout {
    
    [self.mapView selectAnnotation:_annotation animated:YES];
    
}
//接收反向地理编码结果
-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:
(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    if (error == BMK_SEARCH_NO_ERROR) {
    //在此处理正常结果
        self.mapStr = result.address;
    }
    else {
    NSLog(@"抱歉，未找到结果");
    }
}


//地理编码
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
        
        self.annotation = [[BMKPointAnnotation alloc] init];
        CLLocationCoordinate2D coor;
        coor.latitude = result.location.latitude;
        coor.longitude = result.location.longitude;
        self.annotation.coordinate = coor;
        self.annotation.title = @"这里是我家";
        [_mapView addAnnotation:self.annotation];

    }
    else {
        NSLog(@"抱歉，未找到结果");
    }
}



//- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
//{
//    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
//        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
//        newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
//        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
//        return newAnnotationView;
//    }
//    return nil;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
